/*  PLUTO JS -Scripting Web Server.
    Copyright (C) 2013 vivek gangadharan

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package pluto.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import org.jgroups.blocks.ReplicatedHashMap;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.serialize.ScriptableInputStream;
import org.mozilla.javascript.serialize.ScriptableOutputStream;
import static pluto.core.AppLogger.*;

public class Cache {

    private ReplicatedHashMap<String, byte[]> map;
    private ApplicationConfiguration config;
    private Host host;
    
    public Cache(ApplicationConfiguration config,Host host) throws Exception {
        map = new ReplicatedHashMap(config.cluster());
        this.host=host;
    }

    public void distribute() throws Exception {
        try {
            map.start(config.syncRate());
        } catch (Exception e) {
            log.log(Level.SEVERE, "dsitri");
            throw e;
        }
    }

    public void put(String key, Object object) throws IOException {
        if (key == null || object == null) {
            return;
        }
        byte[] valbytes = tobytes(object);
        if (valbytes == null) {
            return;
        }
        map.put(key, valbytes);
    }

    public Object get(String key) throws ClassNotFoundException, IOException {
        if (key == null) {
            return null;
        }
        if (map.containsKey(key)) {
            byte[] valbytes = map.get(key);
            Object object = frombytes(valbytes);
            return object;
        }
        return null;
    }

    public void remove(String key)  {
        if (key == null) {
            return;
        }
         map.remove(key);
    }
    private byte[] tobytes(Object object) throws IOException {
        if (object == null) {
            return null;
        }
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ScriptableOutputStream so = new ScriptableOutputStream(bo, host.global());
            so.writeObject(object);
            return bo.toByteArray();
        } catch (IOException ex) {
            log.log(Level.SEVERE, Localizer.getMessage(Message.CacheSerToByteError), ex);
            throw ex;
        }
    }

    private Object frombytes(byte[] bytes) throws ClassNotFoundException, IOException {
        try {
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ScriptableInputStream si = new ScriptableInputStream(bi,host.global());
            return si.readObject();
        } catch (ClassNotFoundException | IOException ex) {
            log.log(Level.SEVERE, Localizer.getMessage(Message.CacheSerFromByteError), ex);
            throw ex;
        }
    }

     
}
